package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.Tcepc_housing_plan;
import com.sumainfor.pc.epc.feign.Housingauditfeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author Xfma
 * @Date 2018年11月7日
 * @Describe 工程部查询房源配置
 */

@Service
public class Tcepc_housing_planServiceImpl {


    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(Tcepc_housing_planServiceImpl.class);// 日志
    @Autowired
    private Tcepc_housing_plan mapper;
    @Autowired
    private Housingauditfeign feign;

    public  JsonResult getHousingCon(Map<String, Object> params){
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> detmap =new HashMap<String, Object>();
        List<Map<String, Object>> listHousingCon = mapper.getHousingCon(params);//查询房源配置
        if (listHousingCon.size() > 0) {
            for (Map map : listHousingCon) {

                BigDecimal sum = new BigDecimal(mapper.getsumComPrice(map.get("HouConId").toString()));
                sum.setScale(2);
                map.put("sumComPrice",sum);

                //按照房源配置id查询 详情
                List<Map<String, Object>> listmap = mapper.getConInfos(map.get("HouConId").toString());
                map.put("info", listmap);
            }
        }
        detmap.put("Con", listHousingCon);
        jsonResult.put(detmap);


        return  jsonResult;
    }

    public JsonResult getListingDetails(Map<String, Object> params) throws Exception {
        JsonResult jsonResult = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            JsonResult result = feign.getListingDetails(params);
            Map<String, Object> detmap = (Map<String, Object>) result.get("result");//获得
            List<Map<String, Object>> listHousingCon = mapper.getHousingCon(params);//查询房源配置
            if (listHousingCon.size() > 0) {
                for (Map map : listHousingCon) {
                    //按照房源配置id查询 详情
                    List<Map<String, Object>> listmap = mapper.getConInfo(map.get("HouConId").toString());
                    String [] arr=new String []{};

                    if(map.get("StyleId")!=null && !(map.get("StyleId").toString().equals(""))){
                        System.out.println("StyleId================"+map.get("StyleId"));
                        arr=map.get("StyleId").toString().split(",");
                        int[] ints = new int[arr.length];
                        for(int i=0;i<arr.length;i++){
                            System.out.println("arr================"+arr[i]);
                            ints[i] = Integer.parseInt(arr[i]);
                        }
                        map.put("character",ints);
                    }else{
                        map.put("character",arr);
                    }

                    map.put("info", listmap);
                }
            }
            detmap.put("Con", listHousingCon);
            jsonResult.put(detmap);

        } catch (Exception e) {
            log.info("e--->" + e);
            jsonResult.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return jsonResult;
    }

    /**
     * 新增房源配置
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult saveConInfo(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            Integer HouConIds = mapper.getMaxHouConIds(params);//从数据库里面查询出来最大子id
            if (HouConIds != null) {
                HouConIds = HouConIds + 1;
                mapper.updateIsAmend(params);//修改状态
            } else {
                HouConIds = 1;
            }
            params.put("HouConIds", HouConIds);
            String SysUserId = ContextUtils.getUserId();
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            if (mapper.saveConInfo(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 删除某一个房源配置
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult deleteConInfo(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            if (mapper.deleteConInfo(params) > 0) {
                mapper.updateIsAmend(params);//修改状态
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 修改房源配置
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult updateConInfo(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {

            String SysUserId = ContextUtils.getUserId();
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            if (mapper.updateConInfo(params) > 0) {
                mapper.updateIsAmend(params);//修改状态
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }
}
